package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.activity.GuaGiftsConfig.GuaGiftsConfig;
import com.yanqu.road.logic.activity.qixi.QixiConfig;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.guagifts.GuaGiftsMgr;
import com.yanqu.road.server.manger.activity.qixi.QixiMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;

public class GuaGiftsActivityRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        GuaGiftsConfig config = GuaGiftsMgr.getConfig();
        if (config != null) {
            long recycleTime = config.getActivityInfo().getEndTime() * 1000L + 300 * 1000;//领奖期回收
            ActivityItemRecycleMgr.addRecycleItemData(config.getGUAGIFTS_LOVE_LETTER_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());
            ActivityItemRecycleMgr.addRecycleItemData(config.getGUAGIFTS_PAPER_CRANES_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());
            ActivityItemRecycleMgr.addRecycleItemData(config.getGUAGIFTS_JEWELRY_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());

            ActivityItemRecycleMgr.addRecycleItemData(config.getGUAGIFTS_PAPER_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());
            ActivityItemRecycleMgr.addRecycleItemData(config.getGUAGIFTS_SOUR_LEMON_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());
            ActivityItemRecycleMgr.addRecycleItemData(config.getGUAGIFTS_DOG_FOOD_ID(), config.getActivityInfo().getType(), 2, recycleTime, new ArrayList<>());
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.GuaGiftsActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int recycleType = data.getRecycleType();
        String language = UserMgr.getUserInfo(userId).getLanguage();
        UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
        if (userBagItem != null && userBagItem.getNum() > 0) {
            long num = userBagItem.getNum();
            if (removeItem(userBagItem) && recycleType != 0) {
                //物品类别161 花满七夕道具  参数列表：送花数|戒指id|戒指数量|每日赠送上限|是否广播,   赠送上限-1表示无限制，0表示不广播，1广播
                // 转换为道具
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                Property property = new Property();
                if (recycleType == 2) {
                    if (!goodsInfo.getExtendParam().equals("0")) {
                        Property exReward = PropertyHelper.parseStringToProperty(goodsInfo.getExtendParam());
                        exReward.goodCountMultiNum(num);
                        property.addProperty(exReward);
                    }
                }
                //  发邮件
                if (!property.isNothing()){
                    String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                    String mailTitle = MultipleLanguageMgr.getContent(MailManager.GUAGIFTS_MAIL_TITLE, language);
                    StringBuilder removeStr = new StringBuilder();
                    removeStr.append(removeName).append("X").append(num);
                    String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.GUAGIFTS_MAIL_CONTENT,
                            language), removeStr.toString());
                    MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), mailContent, mailTitle);
                }
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
